package com.example.music;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.example.music.Bean.LocalMusicBean;
import com.example.music.DB.PlayHistroyInfo;
import com.example.music.DB.SongInfo;
import com.example.music.DB.UserInfo;
import com.example.music.Utils.LocalMusicAdapter;
import com.example.music.Utils.MusicPlayer;
import com.example.music.Utils.MusicPlaylist;
import com.example.music.Utils.songListAdapter;
import com.example.music.Utils.songListItem;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class profileSongHistoryActivity extends AppCompatActivity {
    MusicPlaylist mDatas;                       //    数据源
    private LocalMusicAdapter localMusicAdapter;//适配器
    private MusicPlayer musicPlayer;
    private ImageView home_btn;
    private ImageView profile_btn;
    private ImageView home_search;
    private ImageView center_btn;
    public void init(){
        musicPlayer = MusicPlayer.getInstance();
        mDatas = MusicPlaylist.getInstance();

        home_btn = findViewById(R.id.home_btn);
        profile_btn = findViewById(R.id.profile_btn);
        home_search = findViewById(R.id.home_search);
        center_btn = findViewById(R.id.center_btn);



        //提取播放记录
        //提取初始播放记录
        /**
         * 1.查询所有
         * 2.按id查询歌列表
         */
        //
//        SharedPreferences sharedPreferences = getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
//        String curUserId = String.valueOf(sharedPreferences.getInt("curUserId",-1));
//        Log.e("err",MusicPlayer.getInstance().getPlaybackHistory().toString());
//        for(LocalMusicBean item: MusicPlayer.getInstance().getPlaybackHistory()){
//            Log.e("err","erro");
//            List<PlayHistroyInfo> list = LitePal.select("UserId,SongId")
//                    .where("UserId=? and SongId=?",curUserId,item.getId())
//                    .find(PlayHistroyInfo.class);
//            if(list.isEmpty()&&Integer.valueOf(curUserId)!=-1) {
//                new PlayHistroyInfo(Integer.valueOf(curUserId), Integer.valueOf(item.getId())).save();
//                MusicPlayer.getInstance().clearPlaybackHistory();
//                Log.e("添加成功","添加成功");
//            }
//        }
//        List<PlayHistroyInfo> list = LitePal.select("UserId,SongId").find(PlayHistroyInfo.class);
//        Log.e("播放列表显示：",list.toString());

    }
    public void initListener(){
        //点击进入主页
        home_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jumpTo(HomeActivity.class);
            }
        });
        profile_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jumpTo(ProfileActivity.class);
            }
        });
        home_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jumpTo(HomeActivity.class);
            }
        });
        center_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jumpTo(MainActivity.class);
            }
        });
    }
    public void initRecycleView() {
        //歌曲
        //主页歌曲
        RecyclerView SongRecyclerView = findViewById(R.id.SongRecyclerView);
        RecyclerView.LayoutManager layoutManager2 = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        SongRecyclerView.setLayoutManager(layoutManager2);

        List<LocalMusicBean> list = new ArrayList<>(MusicPlayer.getInstance().getPlaybackHistory());
        localMusicAdapter = new LocalMusicAdapter(this,list);
        SongRecyclerView.setAdapter(localMusicAdapter);

        // 设置每一项的点击事件
        setEventListener();
    }
    private void setEventListener() {
        /* 设置每一项的点击事件*/
        localMusicAdapter.setOnItemClickListener(new LocalMusicAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                mDatas.setCurrentSongIndex(position);
                LocalMusicBean musicBean = mDatas.getSongs().get(position);
                musicPlayer.addToPlaybackHistory(musicBean);
                musicPlayer.stop();
                musicPlayer.play(musicBean.getPath());
            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_song_history);

        init();
        initListener();
        initRecycleView();
    }
    public void jumpTo(Class context){
        Intent intent = new Intent(profileSongHistoryActivity.this, context);
        startActivity(intent);
    }
}